package com.cloud.file.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @packageName: com.api.util
 * @name: DateUtil
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/6/12 16:50
 */
public class DateUtil {

    /**
     * 锁对象
     */
    private static final Object lockObj = new Object();

    /**
     * 存放不同的日期模板格式的sdf的Map
     */
    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<>();


    /**
     * 返回一个ThreadLocal的sdf,每个线程只会new一次sdf
     *
     * @param pattern
     * @return
     */
    private static SimpleDateFormat getSdf(final String pattern) {
        ThreadLocal<SimpleDateFormat> threadLocal = sdfMap.get(pattern);
        // 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
        if (threadLocal == null) {
            synchronized (lockObj) {
                threadLocal = sdfMap.get(pattern);
                if (threadLocal == null) {
                    // 这里是关键,使用ThreadLocal<SimpleDateFormat>替代原来直接new SimpleDateFormat
                    threadLocal = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern, threadLocal);
                }
            }
        }
        return threadLocal.get();
    }


    /**
     * 日期时间转换为字符串格式
     *
     * @param source  待处理的日期
     * @param pattern 转换的格式
     * @return
     */
    public static String dateToStr(Date source, String pattern) {
        SimpleDateFormat sdf = getSdf(pattern);
        return sdf.format(source);
    }

    /**
     * 字符串转换为日期格式
     *
     * @param source  待处理的字符串
     * @param pattern 转换的格式
     * @return
     */
    public static Date strToDate(String source, String pattern) {
        SimpleDateFormat sdf = getSdf(pattern);
        Date result = null;
        try {
            result = sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String longToStr(long source, String pattern) {
        return dateToStr(new Date(source), pattern);
    }

    public static Date currentDate() {
        return new Date();
    }


    public static String currentDateTimeStr() {
        return dateToStr(new Date(), "yyyyMMddHHmmss");
    }

    public static String currentYearStr() {
        return dateToStr(new Date(), "yyyy");
    }

    public static String currentYearMonthStr() {
        return dateToStr(new Date(), "yyyyMM");
    }

    public static String currentDateStr() {
        return dateToStr(new Date(), "yyyyMMdd");
    }

    public static long currentMillis() {
        return System.currentTimeMillis();
    }

    public static int currentSeconds() {
        return (int) (currentMillis() / 1000L);
    }

    public static int addTimeToSecond(int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(field, amount);
        return (int) (calendar.getTime().getTime() / 1000L);
    }
}
